package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.OrdersStatusEnum;
import com.qinggeng.common.enums.PrintStatusEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.config.mq.dto.MessageOrdersDTO;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.MQSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OrdersPrintOutTimeHandleImpl implements MessageHandle {
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private MQSendService mqSendService;
    @Override
    public void handle(String msg) {

        log.info("订单打印超时处理{}"+msg);

        MessageOrdersDTO ordersDTO= JSON.parseObject(msg,MessageOrdersDTO.class);

        Orders orders=ordersService.selectOrdersByOrdersNo(ordersDTO.getOrdersNo());

        if(null==orders){
            throw new CustomException("订单不存在");
        }
        if(!orders.getStatus().equals(OrdersStatusEnum.进行中.getValue())){
            return;
        }
        if(!orders.getPrintStatus().equals(PrintStatusEnum.不可打印.getStatus())){
            return;
        }
        //将00=不可打印更新为10=可打印
        boolean result=ordersService.updateOrdersPrintStatus(orders,"00","10");
        if(!result){
            log.error("订单打印状态更新失败");
            throw new CustomException("订单打印状态更新失败");
        }
        //发送自动发货消息
        //mqSendService.sendOrdersAutoShipDelayMessage(orders.getOrdersNo());
    }
}
